/*
 * app_tts_vioce.c
 *
 *  Created on: 2022年9月17日
 *      Author: Administrator
 */

#include "app_common.h"

#if (TTS_ENABLE == STD_ON)

#include "app_tts_voice.h"
#include "driver/i2s.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "esp_tts_voice.h"
#include "esp_tts.h"
#include "esp_tts_voice_xiaole.h"
//#include "esp_tts_voice_female_digits.h"
#include "board.h"

#include "i2c_bus.h"
#include "es8311.h"


#define TAG                  ("app_tts_voice")


static void Audio_Tts_Play(const uint8_t *data, int length, TickType_t ticks_to_wait);
static void Tts_OutputChinese(esp_tts_handle_t *tts_handle,  char *data);

void Audio_Init(void)
{
	ESP_ERROR_CHECK(i2c_bus_init());

    es8311_init(SAMPLE_RATE_DEFAULT);
    es8311_set_voice_volume(VOL_VAL_DEFAULT);
    /*!<  for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes or 8-bytes each sample) */
    /*!<  depend on bits_per_sample */
    /*!<  using 6 buffers, we need 60-samples per buffer */
    /*!<  if 2-channels, 16-bit each channel, total buffer is 360*4 = 1440 bytes */
    /*!<  if 2-channels, 24/32-bit each channel, total buffer is 360*8 = 2880 bytes */
    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX,                                  /*!<  Only TX */
        .sample_rate = 16000,
        .bits_per_sample = 16,
        .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,                           /*!< 1-channels */
        .communication_format = I2S_COMM_FORMAT_STAND_I2S,
        .dma_buf_count = 6,
        .dma_buf_len = 256,
        .use_apll = true,
        // .tx_desc_auto_clear = true,     /*!< I2S auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) */
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM,
    };
    i2s_pin_config_t pin_config = {
        .bck_io_num = I2S_SCLK,
        .ws_io_num = I2S_LCLK,
        .data_out_num = I2S_DOUT,
        .data_in_num = I2S_DSIN                                          /*!< Not used */
    };

    i2s_driver_install(I2S_STANCE, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_STANCE, &pin_config);
}

// 喇叭播放声音，写I2S
static void Audio_Tts_Play(const uint8_t *data, int length, TickType_t ticks_to_wait)
{
    size_t bytes_write = 0;

    i2s_write(0, (const char *) data, length, &bytes_write, ticks_to_wait);
}

// 播放汉字处理程序
static void Tts_OutputChinese(esp_tts_handle_t *tts_handle,  char *data)
{
    if (esp_tts_parse_chinese(tts_handle, data)) {
        int len[1] = {0};

        do {
            short *data = esp_tts_stream_play(tts_handle, len, 4);
            Audio_Tts_Play((const uint8_t*)data, len[0] * 2, portMAX_DELAY);
            // printf("data:?%d \n", len[0]);
        } while (len[0] > 0);

        i2s_zero_dma_buffer(0);
    }
}

// 播放文字语音
void Audio_PlayerChinese(char *sentence)
{
    esp_tts_voice_t *voice = (esp_tts_voice_t *)&esp_tts_voice_xiaole;
    esp_tts_handle_t *tts_handle = esp_tts_create(voice);

    ESP_LOGI(TAG, "sentence = %s", sentence);
    Tts_OutputChinese(tts_handle, sentence);
}

#endif
